<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>地址管理</title>

		<link rel="stylesheet" type="text/css" href="../css/mui.min.css" />
		<link rel="stylesheet" type="text/css" href="../css/own.css" />
		<link rel="stylesheet" type="text/css" href="../css/iconfont.css" />
		<link rel="stylesheet" type="text/css" href="../css/mui.picker.min.css" />

		<style type="text/css">
			.mui-input-row label {
				width: 100%;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav own-main-background-color">
			<button type="button" class="mui-action-back mui-btn  mui-btn-link mui-pull-left own-button">
			<!--<span class="mui-action-back mui-icon mui-icon-left-nav own-left-nav-color"></span>-->
			<span class="own-left-nav-color">返回</span>
		</button>
			<h1 class="mui-title">地址管理</h1>
		</header>

		<div class="mui-content">
			<form class="mui-input-group">
				<div class="mui-input-row">
					<input type="text" class="mui-input-clear" placeholder="服务对象姓名" id="name" value="" />
				</div>
				<div class="mui-input-row">
					<input type="text" class="mui-input-clear" placeholder="手机号码" id="phoneNum">
					
				</div>
				<div class="mui-input-row" id="addressPicker" >
					<label id="dizhiya">省、市、区</label>
				</div>
				<div class="mui-input-row" style="height: 4em; padding-top: 2px;">
					<textarea placeholder="详细地址" id="dizhi4"></textarea>
					<input type="text" class="mui-input-clear"  id="addressId" hidden="hidden">
				</div>
			</form>
		</div>

		<nav class="mui-bar mui-bar-tab" style="height: 50px;">
			<div class="mui-row">
				<div class="mui-input-row mui-checkbox mui-left mui-col-sm-9 mui-col-xs-9">

					<label style="line-height: 50px;font-size: 20px;">设为默认地址</label>
					<input name="checkbox1" value="Item 1" type="checkbox">

				</div>

				<div class="mui-col-sm-3 mui-col-xs-3" style="padding-top: 10px;">
					<button type="button" class="mui-btn mui-btn-success" style="font-size: 20px;" id="sub">确认</button>
				</div>
			</div>

		</nav>
		<script type="text/javascript" src="../js/jquery-1.11.1.min.js" ></script>
		<script src="../js/mui.min.js" charset="UTF-8"></script>
		<script src="../js/city.data-3.js" charset="UTF-8"></script>
		<script src="../js/mui.picker.min.js" charset="UTF-8"></script>
		<script src="../js/own.js" charset="UTF-8"></script>
		<script type="text/javascript" charset="UTF-8">
			mui.init({
				swipeBack: true,

				//document.getElementById("name").value= plus.storage.getItem("myOrderName");;
				//document.getElementById("myOrderPhone").value= myOrderPhone;
				//document.getElementById("name").value= plus.storage.getItem("myOrderName");
				// document.getElementById("myOrderAddr").myOrderAddr;
				
				beforeback: function(){
		//获得列表界面的webview
		var list = plus.webview.getWebviewById('template-sub-first.html');
		//触发列表界面的自定义事件（refresh）,从而进行数据刷新
		mui.fire(list,'refresh');
		
		//返回true，继续页面关闭逻辑
		return true;
	}

			});

			var addressPicker;
			var pickerlabel;
			//var myOrderName;
			//var myOrderPhone;
			//var myOrderAddr;

			mui.plusReady(function() {

				var self = plus.webview.currentWebview();
				var name = self.name;
				var phoneNum = self.phoneNum;
				var dizhi1=self.dizhi1;
				var dizhi2=self.dizhi2;
				var dizhi3=self.dizhi3;
				var dizhi4=self.dizhi4;
				var addressId=self.addressId;
				
				document.getElementById("name").value = name;
				document.getElementById("phoneNum").value = phoneNum;
				
				document.getElementById("addressPicker").children[0].innerText= dizhi1+ " " +dizhi2+ " " +dizhi3;
				
				document.getElementById("dizhi4").value= dizhi4;
				document.getElementById("addressId").value= addressId;
				
				
				
				//alert(document.getElementById("dizhi4").children[0].innerText);

				//var web = plus.webview.currentWebview();
				//alert(web.title);
				//plus.storage.setItem("myAddressId",addressId);
				//plus.storage.setItem("myOrderName",name);
				//plus.storage.setItem("myOrderPhone",phoneNum);
				//plus.storage.setItem("myOrderAddr",address); 
				//alert(plus.storage.getItem("myOrderName"));   
				// myOrderName = plus.storage.getItem("myOrderName");
				// myOrderPhone = plus.storage.getItem("myOrderPhone");
				//document.getElementById("name").value= plus.storage.getItem("myOrderName");
				// myOrderAddr= plus.storage.getItem("myOrderAddr");

				addressPicker = document.getElementById('addressPicker');
				pickerlabel = addressPicker.children[0];

				//监听呼出picker事件
				addressPicker.addEventListener('tap', function() {
					var cityPicker3 = new mui.PopPicker({
						layer: 3
					});
					cityPicker3.setData(cityData3);
					cityPicker3.show(function(items) {
						pickerlabel.innerText = (items[0] || {}).text + " " + (items[1] || {}).text + " " + (items[2] || {}).text;
					});
				}, false);
						
						
						var btn = document.getElementById("sub");
					//监听点击事件
					btn.addEventListener("tap",function () {
						//var namename=document.getElementById("name").value;
						var phoneNumphoneNum=document.getElementById("phoneNum").value;
						//var dizhiya=document.getElementById("dizhiya").value;
						var addressId=document.getElementById("addressId").value;
						var phoneNum1=document.getElementById("phoneNum").value;
						var re = /^[1][3587]\d{9}$/;
					
					/*	if(namename==""){
							alert("内容不能为空");
							return false;
						}*/
						
				/*	else if(phoneNumphoneNum==""){
							alert("手机号码不能为空");
							return false;
						}*/
						 if(pickerlabel.innerText==""){
							alert("地址不能为空");
							return false;
						}
						
						else if(addressId==""){
							alert("详细地址不能为空");
							return false;
						}
						
					else if(!re.test(phoneNum1)) {
					alert("电话错误");
					return false;
				}
					 
					 	var data = {};
		data.tokenCode =  plus.storage.getItem("tokenCode");
		data.accountId = plus.storage.getItem("accountId");
		data.addressId = addressId;
		data.name = document.getElementById("name").value;
		data.address = document.getElementById("dizhi4").value;
		data.phone = document.getElementById("phoneNum").value;
		
		var str = document.getElementById("addressPicker").children[0].innerText.split(" ");
		
		data.province = str[0];
		data.city = str[1];
		data.area = str[2];
		
		
		
		$.ajax({
			url: url+"rest/updateUserAddress",
			type: "POST",
			data: data,
			success: function(result){
				//返回json字符串，转成json对象进行属性获取
				//{"status":"value","msg":"提示信息"}
				// status 为 0  表示 调用成功，为其他值则为失败
				result = JSON.parse(result);
				$("#resultCode").val(result.status);
				$("#resultMsg").val(result.msg);
				if(result.status != '0'){
					
					mui.alert(result.msg);
				}else{
					mui.toast("成功更新地址");
					mui.back();
					
					
				}
			}
		});
					 
					 
					});
				
				//接收rightbar事件
				/*window.addEventListener('navRightPress', function() {
					mui.toast('选择了确定');
					//上报服务器e3-server
					mui.back();
				}, false)*/
			});

			//		if(window.plus) {
			//			plusReady();
			//			} else {
			//			document.addEventListener("plusready", plusReady, false);
			//			}

			
		</script>

	</body>

</html>